class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummy = new ListNode(0, head);
        ListNode* cur = dummy;

        int count = 0;

        while (head)
        {
            ++count;
            head = head->next;
        }
        cout << count << endl;
        for (int i = 1; i <= count - n; i++)
        {
            cur = cur->next;
        }
        cout << cur->val << endl;
        cur->next = cur->next->next;
        ListNode* ans = dummy->next;
        delete dummy;
        return ans;

        return head;
    }
};